package com.salim.leetcode.$17;

import java.util.*;

public class LetterCombinationsOfAPhoneNumber {
    private static Character[][] digitsArray = new Character[][]{
            {'a','b','c'},
            {'d','e','f'},
            {'g','h','i'},
            {'j','k','l'},
            {'m','n','o'},
            {'p','q','r','s'},
            {'t','u','v'},
            {'w','x','y','z'},
    };
    public List<String> letterCombinations(String digits) {
        if(digits.length()==0){
            return new ArrayList<>();
        }
        List<String> result = new ArrayList<>();

        Character[] array = digitsArray[digits.charAt(0)-48-2];
        for(int i=0;i<array.length;i++){
            loop(new StringBuilder(array[i].toString()),digits,1,result);
        }

        return result;
    }

    private void loop(StringBuilder base, String source, Integer index, List<String> result){
        if(index==source.length()){
            result.add(base.toString());
        }else{
            Character[] array = digitsArray[source.charAt(index)-48-2];
            index++;
            for(int i=0;i<array.length;i++){
                loop(new StringBuilder(base).append(array[i]),source,index,result);
            }
        }
    }

    public static void main(String[] args){
        LetterCombinationsOfAPhoneNumber letter = new LetterCombinationsOfAPhoneNumber();
        System.out.println(letter.letterCombinations("2345"));
    }
}
